import { BackMenu, backMenuToMenuData, backMenuToRoutes } from '@/utils/menu';
import { MenuDataItem } from '@ant-design/pro-layout';

export function mergeStaticWithDynamic(
  staticRoutes: any[],
  backFlat: BackMenu[],
) {
  const dynamicRoutes = backMenuToRoutes(backFlat);
  const dynamicMenuData = backMenuToMenuData(backFlat);
  const root = staticRoutes.find(r=>r.path === '/')
  if(root) root.routes.unshift(...dynamicRoutes);
  else staticRoutes.unshift(...dynamicRoutes)

  const staticMenuData:MenuDataItem[] = staticRoutes
    .filter(r=>r.name)
    .map(r=>({
    path:r.path,
      name:r.name,
      icon:r.icon,
      children:r.routes?.filter((c:any)=>c.name)
        .map((c:any)=>({path:c.path,name:c.name,icon:c.icon}))
  }))
  console.log(staticMenuData);
  const mergedMenuData = [
    ...staticMenuData,
    ...dynamicMenuData
  ]
  return {routes:staticRoutes,menuData:mergedMenuData};
}
